﻿package util {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.filters.ColorMatrixFilter;
	import flash.events.Event;
	
	public class MsgBox extends Sprite {
		private var box:TextField;
		private var count:int;
		private var timeCount:int;
		private var tarY:int;
		
		public function MsgBox():void {
			initView();
			count = 16;
			timeCount = 0;
			visible = false;
		}
		
		private function initView():void {
			box = getChildByName("msg") as TextField;
			box.background = true;
			box.border = Config.MSG_BORDER;
			box.borderColor = Config.MSG_BORDER_COLOR;
			//使textfield有透明效果
			box.filters = [new ColorMatrixFilter()];
		}
		
		public function resetY(n:int):void {
			tarY = y = n;
		}
		
		public function showMsg(s:String):void {
			box.backgroundColor = Config.MSG_BG_COLOR;
			show(s);
		}
		public function showAlert(s:String):void {
			box.backgroundColor = Config.MSG_ALERT_COLOR;
			show(s);
		}
		private function show(s:String):void {
			box.htmlText = s;
			box.width = box.textWidth + 4;
			alpha = 0;
			visible = true;
			y = tarY;
			count = 16;
			removeEventListener(Event.ENTER_FRAME, onCountHandler);
			removeEventListener(Event.ENTER_FRAME, onDownHandler);
			addEventListener(Event.ENTER_FRAME, onUpHandler);
		}
		
		private function onUpHandler(evt:Event):void {
			if (count-- > -1) {
				y--;
				alpha += 0.05;
			}
			else {
				timeCount = 0;
				removeEventListener(Event.ENTER_FRAME, onUpHandler);
				addEventListener(Event.ENTER_FRAME, onCountHandler);
			}
		}
		private function onCountHandler(evt:Event):void {
			//计数器>60后自动隐藏提示框
			if (timeCount++ > 60) {
				removeEventListener(Event.ENTER_FRAME, onCountHandler);
				addEventListener(Event.ENTER_FRAME, onDownHandler);
			}
		}
		private function onDownHandler(evt:Event):void {
			if (count ++ < 17) {
				y++;
				alpha -= 0.05;
			}
			else {
				removeEventListener(Event.ENTER_FRAME, onDownHandler);
			}
		}
	}
}